package com.leyou.search.Controller;

import com.leyou.common.pojo.PageResult;
import com.leyou.search.Service.SearchService;
import com.leyou.search.pojo.Goods;
import com.leyou.search.pojo.SearchRequest;
import com.leyou.search.pojo.SearchResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping
public class SearchController {

    @Autowired
    private SearchService searchService;

    @PostMapping("page")
    public ResponseEntity<SearchResult> search(@RequestBody SearchRequest searchRequest) {
        //设置分页默认页面大小
        searchRequest.setSize(20);
        SearchResult search = this.searchService.search(searchRequest);
        Map<String, String> filter = searchRequest.getFilter();
        Set<Map.Entry<String, String>> entries = filter.entrySet();
        if (search == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(search);
    }

    /**
     * 分页猜你喜欢
     *
     * @return
     */
    @PostMapping("like")
    public ResponseEntity<PageResult<Goods>> guessYouLike(@RequestBody SearchRequest searchRequest) {
        PageResult<Goods> pageResult = this.searchService.guessYouLike(searchRequest);
        if (pageResult == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(pageResult);
    }

    @PostMapping("love")
    public ResponseEntity<List<Goods>> guessYouLove(@RequestBody SearchRequest request) {
        List<Goods> loveGoods = this.searchService.guessYouLove(request);
        if (loveGoods == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(loveGoods);
    }

}
